package com.woniuxy.commons.config;

import com.woniuxy.commons.filter.PhoneLoginAuthenticationFilter;
import com.woniuxy.commons.handler.LoginErrorHandler;
import com.woniuxy.commons.handler.LoginSuccessHandler;
import com.woniuxy.service.AccountInfoService;
import com.woniuxy.service.CustomerInfoService;
import com.woniuxy.service.StoreInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;



@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private LoginSuccessHandler loginSuccessHandler;

    @Autowired
    private LoginErrorHandler loginErrorHandler;

    @Autowired
    private StringRedisTemplate srt;

    @Autowired
    private AccountInfoService accountInfoService;

    @Autowired
    private StoreInfoService storeInfoService;

    @Autowired
    private CustomerInfoService customerInfoService;

    @Bean
    public PasswordEncoder createEncoder() {
        return new BCryptPasswordEncoder();
    }

    public UrlBasedCorsConfigurationSource createUrlBasedCorsConfigurationSource(){
        UrlBasedCorsConfigurationSource cfs = new UrlBasedCorsConfigurationSource();
        CorsConfiguration cors = new CorsConfiguration();
        cors.addAllowedOriginPattern("*");
        cors.addAllowedMethod("*");
        cors.setAllowCredentials(true);
        cors.addAllowedHeader("*");
        cfs.registerCorsConfiguration("/**", cors);
        return cfs;
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        PhoneLoginAuthenticationFilter phoneFilter = new PhoneLoginAuthenticationFilter(accountInfoService,storeInfoService,customerInfoService,srt);

        phoneFilter.setAuthenticationSuccessHandler(loginSuccessHandler);

        phoneFilter.setAuthenticationFailureHandler(loginErrorHandler);


        //关闭基础认证
        http.addFilterAfter(phoneFilter, UsernamePasswordAuthenticationFilter.class)
                .httpBasic().disable()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/swagger-resources/**","/swagger-ui.html","/v2/api-docs","/webjars/**","/test","/login.html","/login","/sendCode/*","/errorlogin","/phoneLogin","/redis/**","/myStoreRegister").permitAll()
                .antMatchers("/dietOrder/notify","/register").permitAll()
                .antMatchers("/storeInfo/**", "/productsInfo/**", "/foodVariety/**", "/foodMonthSale/**", "/swagger-resources/**", "/swagger-ui.html", "/v2/api-docs", "/webjars/**", "/test", "/login.html", "/login", "/sendCode/*", "/errorlogin", "/phoneLogin", "/redis/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/login")
                .loginPage("/errorlogin")
                .successHandler(loginSuccessHandler)
                .failureHandler(loginErrorHandler)
                .and()
                .cors()
                .configurationSource(createUrlBasedCorsConfigurationSource()).and()
                .logout();


    }


}
